﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
#if CWM_DEV
        [CtxMenuItem("Renderer/Mesh/Normals/Stylized A", "Experimental Stylized Normals", 0)]
        public static void RendererNormalsStylized()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Stylize Normals");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                var triangles = mesh.triangles;
                var tangents = mesh.GetTangentsRecalculated();
                var normals = mesh.GetNormalsSmoothed();

                for (int i = 0; i < triangles.Length; i += 3)
                {
                    int a = triangles[i];
                    normals[a] = Vector3.Lerp(normals[a], ((-(Vector3)tangents[a] * 0.1f) + normals[a]).normalized, 1);
                }
                mesh.normals = normals;
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();

        }

        [CtxMenuItem("Renderer/Mesh/Normals/Stylized B", "Experimental Stylized Normals", 0)]
        public static void RendererNormalsStylizedB()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Stylize Normals");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                var triangles = mesh.triangles;
                var tangents = mesh.GetTangentsRecalculated();
                var normals = mesh.GetNormalsSmoothed();

                for (int i = 0; i < triangles.Length; i += 3)
                {
                    int a = triangles[i + 1];
                    normals[a] = Vector3.Lerp(normals[a], ((-(Vector3)tangents[a] * 0.1f) + normals[a]).normalized, 1);

                }
                mesh.normals = normals;
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();

        }


        [CtxMenuItem("Renderer/Mesh/Normals/Stylized C", "Experimental Stylized Normals", 0)]
        public static void RendererNormalsStylizedC()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Stylize Normals");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                var triangles = mesh.triangles;
                var tangents = mesh.GetTangentsRecalculated();
                var normals = mesh.GetNormalsSmoothed();

                for (int i = 0; i < triangles.Length; i += 3)
                {
                    int a = triangles[i + 2];
                    normals[a] = Vector3.Lerp(normals[a], ((-(Vector3)tangents[a] * 0.1f) + normals[a]).normalized, 0.5f);
                }
                mesh.normals = normals;
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();

        }
#endif
    }
}